var gulp=require("gulp");
var imagemin=require("gulp-imagemin");
// var pngquant=require('imagemin-pngquant');
var spritesmith=require('gulp.spritesmith');
var minimist=require('minimist');
var buffer = require('vinyl-buffer');
var csso = require('gulp-csso');
var rename = require("gulp-rename");
var replace = require("gulp-replace");
var merge = require('merge-stream');
// var watch = require('gulp-watch');
var livereload = require('gulp-livereload');
var sass = require('gulp-ruby-sass');
var webserver = require('gulp-webserver');

var uglify = require('gulp-uglify'); // js压缩
var minifyCss = require('gulp-minify-css');

var concat = require("gulp-concat"); // 文件合并
var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');

var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');

/**
 * 将多个css文件压缩到指定文件中去
 */
gulp.task('minify-css', function() {
  return gulp.src(['./css/animate.min.css','./css/20160307whzf.css'])
    .pipe(minifyCss())
    .pipe(concat('lib.css'))
    .pipe(gulp.dest('./dist/css'));
});
gulp.task('script', function() {
  return gulp.src(['./js/index.js']) // 指明源文件路径、并进行文件匹配
    .pipe(uglify({ preserveComments:'some' })) // 使用uglify进行压缩，并保留部分注释
    .pipe(concat('lib.js'))
    .pipe(gulp.dest('./dist/js')); // 输出路径
});




// gulp.task('default',['imagemin','sprite']);
var options=minimist(process.argv.slice(2));
// 键入格式为


gulp.task('default',['webserver','watch']);
gulp.task('webserver', function() {
  gulp.src( '.' ) // 服务器目录（./代表根目录）
  .pipe(webserver({ // 运行gulp-webserver
  	host:'localhost',
  	port:'9000',
  	'directoryListing':true,
    livereload: true, // 启用LiveReload
    root: 'src', // 制定工作目录
    open: 'src/html/index.html' // 服务器启动时自动打开网页
  }));
});
gulp.task('mySprite', function () {
  console.log('正在生成雪碧图');
 // Generate our spritesheet
  var spriteData = gulp.src('./src/images/sprites/icon*.png').pipe(spritesmith({
     imgName: 'sprite.png',
     cssName: '_sprite.scss',
     cssFormat: 'scss',
	  algorithm: 'binary-tree',
	  padding: 8,
	  cssTemplate:'scss.template.mustache'

  }));
  // Pipe image stream through image optimizer and onto disk
  // DEV: We must buffer our stream into a Buffer for `imagemin`
  var imgStream = spriteData.img
    .pipe(buffer())
    // .pipe(imagemin().on('error', function (err) {
    //        console.error('Error!', err.message); // 显示错误信息
    //   }))
    .pipe(gulp.dest('./src/images/sprites'));

  // Pipe CSS stream through CSS optimizer and onto disk
  var cssStream = spriteData.css
    .pipe(gulp.dest('./src/sass/block/'));
// console.log(spriteData.img)

  // Return a merged stream to handle both `end` events
  return merge(imgStream, cssStream);

});

gulp.task('myHtmlandJS', function () {
    return gulp.src(['src/html/*.html','src/js/*'])
    .pipe(livereload());
});
gulp.task('mySass', function () {
	 var processors = [
        autoprefixer({browsers: ["Android 4.1", "iOS 7.1", "Chrome > 31", "ff > 31", "ie >= 7"]}),
        // cssnano(),
    ];
      sass('./src/sass/*.scss', {sourcemap: false,style: 'compact' }) // 指明源文件路径、并进行文件匹配（style: 'compressed' 表示输出格式）
          .on('error', function (err) {
               console.error('Error!', err.message); // 显示错误信息
          })
		     // For file sourcemaps
		    .pipe(sourcemaps.write('./'))
		    .pipe(postcss(processors))
        .pipe(gulp.dest('./src/css/'))
        .pipe(livereload()); // 输出路径
});
gulp.task('watch',function(){
		console.log('监听路径src');
	livereload.listen();
	gulp.watch(['./src/html/*','./src/js/*'],['myHtmlandJS'],function(){
		console.log("html has changed");
	});
	gulp.watch(['./src/sass/*.scss','./src/sass/block/*.scss'],['mySass']);
	gulp.watch('./src/images/sprites/*.png',['mySprite','mySass']);

});

var zy_pc=['zy_index_pc']

 gulp.task('dist-zy',function () {
       console.log('开始进行打包操作')
       gulp.src('./src/html/zy*.html').pipe(gulp.dest('./dist/zy/html'))
       console.log('完成泽元PC的模板复制')
       gulp.src('./src/css/zy*.css').pipe(gulp.dest('./dist/zy/css'))
       console.log('完成泽元PC的css复制')
       gulp.src('./src/js/*.js').pipe(gulp.dest('./dist/zy/js'))
       console.log('完成泽元PC的js复制')
       gulp.src('./src/images/*').pipe(gulp.dest('./dist/zy/images'))
       console.log('完成泽元PC的images复制')
})

